package com.gdyx.app.web.svc.impl;

import com.gdyx.app.web.dao.MpsFunDao;
import com.gdyx.app.web.dao.MpsFunOptDao;
import com.gdyx.app.web.dao.MpsRoleFunDao;
import com.gdyx.app.web.dao.MpsRoleOptDao;
import com.gdyx.app.web.model.MpsFun;
import com.gdyx.app.web.model.MpsFunOpt;
import com.gdyx.app.web.model.MpsRoleFun;
import com.gdyx.app.web.model.MpsRoleOpt;
import com.gdyx.app.web.svc.MpsFunSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Y_Coffee on 2017/8/3.
 */
@Service
public class MpsFunSvcImpl implements MpsFunSvc {
    @Autowired
    MpsFunDao dao;
    @Autowired
    MpsRoleFunDao mpsRoleFunDao;
    @Autowired
    MpsFunOptDao mpsFunOptDao;
    @Autowired
    MpsRoleOptDao mpsRoleOptDao;

    @Override
    public void save(MpsFun o) {
        dao.save(o);
    }

    @Override
    public void saveOrUpdate(MpsFun o) {
        dao.saveOrUpdate(o);
    }

    @Transactional
    @Override
    public void delete(String id) {
        mpsRoleFunDao.deleteByFid(id);
        mpsFunOptDao.deleteByFid(id);
        dao.delete(id);
    }

    @Override
    public void update(MpsFun o) {
        dao.update(o);
    }

    @Override
    public MpsFun findMpsFunById(String id) {
        return dao.findMpsFunById(id);
    }

    @Override
    public MpsFun findMpsFunByHref(String href) {
        return dao.findMpsFunByHref(href);
    }


    @Override
    public int count(Map<String, Object> map) {
        return dao.count(map);
    }

    @Override
    public List<MpsFun> findMpsFuns(Map<String, Object> map) {
        return dao.findMpsFuns(map);
    }

    @Transactional
    @Override
    public void setAuthority(String roleId, String ids, String Ownerid, List<MpsFun> mpsFunList, List<MpsFunOpt> mpsFunOptList) {
        mpsRoleFunDao.deleteByRid(roleId);
        mpsRoleOptDao.deleteByRid(roleId);
        if (StringUtils.isEmpty(ids)) {
            return;
        }
        String[] idArray = ids.split(",");
        for (String fId : idArray) {
            if (fId.contains("-")) {
                if (funContains(mpsFunList, fId)) {
                    MpsRoleFun mpsRoleFun = new MpsRoleFun();
                    mpsRoleFun.setId(UUID.randomUUID().toString().toUpperCase().trim());
                    mpsRoleFun.setFunId(fId);
                    mpsRoleFun.setRoleId(roleId);
                    mpsRoleFun.setModifyOwner(Ownerid);
                    mpsRoleFun.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                    mpsRoleFunDao.save(mpsRoleFun);
                } else {
                    System.out.println("出错！不允许配置已有权限以外的功能");
                }
            } else {
                if (funOptContains(mpsFunOptList, fId)) {
                    MpsRoleOpt mpsRoleOpt = new MpsRoleOpt();
                    mpsRoleOpt.setId(UUID.randomUUID().toString().toUpperCase().trim());
                    mpsRoleOpt.setOptId(fId);
                    mpsRoleOpt.setRoleId(roleId);
                    mpsRoleOpt.setModifyOwner(Ownerid);
                    mpsRoleOpt.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                    mpsRoleOptDao.save(mpsRoleOpt);
                } else {
                    System.out.println("出错！不允许配置已有权限以外的操作");
                }
            }
        }

    }

    public boolean funContains(List<MpsFun> mpsFunList, String fId) {
        boolean Authorityfilter = false;
        for (MpsFun mpsFun : mpsFunList) {
            if (mpsFun.getId().equals(fId)) {
                Authorityfilter = true;
            }
        }
        return Authorityfilter;
    }

    public boolean funOptContains(List<MpsFunOpt> mpsFunOptList, String fId) {
        boolean Authorityfilter = false;
        for (MpsFunOpt mpsFunOpt : mpsFunOptList) {
            if (mpsFunOpt.getId().equals(fId)) {
                Authorityfilter = true;
            }
        }
        return Authorityfilter;
    }


}
